﻿using Consts;
using Dto;
using Scenes;
using Scenes.Msg;
using UI;
using UnityEngine;
using UnityEngine.UI;

public class MatchPanel : UIBase
{
    private Button _btnMatch;
    private Image _imgBg;
    private Text _txtDes;
    private Button _btnCanel;
    private Button _btnEnter;
    private ReqBase _reqBase ;


    private void Awake()
    {
        Bind(UIEvent.SHOW_ENTER_ROOM_BUTTON);
    }

    public override void Execute(int eventCode, object message)
    {
        switch (eventCode)
        {
            case UIEvent.SHOW_ENTER_ROOM_BUTTON:
                // TODO
                _btnEnter.gameObject.SetActive(true);
                break;
        }
    }

    // Start is called before the first frame update
    void Start()
    {
        _btnMatch = transform.Find("BtnMatch").GetComponent<Button>();
        _imgBg = transform.Find("ImgBg").GetComponent<Image>();
        _txtDes = transform.Find("TxtDes").GetComponent<Text>();
        _btnCanel = transform.Find("BtnCancel").GetComponent<Button>();
        _btnEnter = transform.Find("BtnEnter").GetComponent<Button>();
        _btnMatch.onClick.AddListener(MatchClick);
        _btnCanel.onClick.AddListener(CancelClick);
        _btnEnter.onClick.AddListener(EnterClick);
        ObjectsActive(false);
        _btnEnter.gameObject.SetActive(false);
        _reqBase = new ReqBase(SocketConst.MATCH);
    }

    private void EnterClick()
    {
        Dispatch(AreaCode.SCENE,SceneEvent.LOAD_SCENE,new LoadSceneMsg(2, null));
    }

    private void Update()
    {
        if (_txtDes.gameObject.activeInHierarchy == false)
            return;
        _timer += Time.deltaTime;
        if (_timer >= _intervalTime)
        {
            doAnimation();
            _timer = 0;
        }
    }

    public override void OnDestroy()
    {
        base.OnDestroy();
        _btnMatch.onClick.RemoveListener(MatchClick);
        _btnCanel.onClick.RemoveListener(CancelClick);

    }

    private void MatchClick()
    {
        //TODO  向服务器发起匹配请求   
        _reqBase.Change(MatchCodeConst.ENTER,null);
        Dispatch(AreaCode.NET,0,_reqBase);
        ObjectsActive(true);
    }

    private void CancelClick()
    {
        _reqBase.Change(MatchCodeConst.LEAVE,null);
        //向服务器发起离开匹配的请求
        Dispatch(AreaCode.NET, 0, _reqBase);

        setPanelActive(false);

        //按钮隐显示
        this._btnMatch.interactable = true;
    }

    /**
     * 控制点击匹配按钮之后的显示的物体
     */
    private void ObjectsActive(bool active)
    {
        _timer = 0;
        _dotCount = 0;
        _txtDes.text = _defaultText;
        _imgBg.gameObject.SetActive(active);
        _txtDes.gameObject.SetActive(active);
        _btnCanel.gameObject.SetActive(active);
    }

    private string _defaultText = "正在寻找房间";
    private int _dotCount = 0;


    // 动画间隔时间
    private float _intervalTime = 0.3f;
    private float _timer = 0;

    /**
     * 做动画
     */
    private void doAnimation()
    {
        _txtDes.text = _defaultText;
        _dotCount++;
        if (_dotCount > 5)
            _dotCount = 1;
        for (int i = 0; i < _dotCount; i++)
        {
            _txtDes.text += ".";
        }
    }
}